﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuYiAdmin.Net.Common.Utility;
using RuYiAdmin.Net.Entity.BusinessEntity.SystemManagement;
using RuYiAdmin.Net.Entity.BusinessEnum;
using RuYiAdmin.Net.Entity.CoreEntity;
using RuYiAdmin.Net.Entity.CoreEnum;
using RuYiAdmin.Net.Service.BusinessService.SystemManagement.Organization;
using RuYiAdmin.Net.WebApi.AppCode.ActionFilters;
using RuYiAdmin.Net.WebApi.AppCode.FrameworkBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RuYiAdmin.Net.WebApi.Controllers.SystemManagement
{
    /// <summary>
    /// 机构管理控制器
    /// </summary>
    public class OrganizationManagementController : BaseController<SysOrganization>
    {
        /// <summary>
        /// 机构接口实例
        /// </summary>
        private readonly IOrganizationService organizationService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="organizationService"></param>
        public OrganizationManagementController(IOrganizationService organizationService) : base(organizationService)
        {
            this.organizationService = organizationService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns>QueryResult</returns>
        [HttpPost]
        [Log(OperationType.QueryList)]
        [Permission("org:query:list")]
        public IActionResult Post(QueryCondition queryCondition)
        {
            var actionResult = this.organizationService.GetOrgTreeNodes();
            return Ok(actionResult);
        }

        /// <summary>
        /// 查询机构
        /// </summary>
        /// <param name="orgId">机构编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{orgId}")]
        [Log(OperationType.QueryEntity)]
        [Permission("org:query:list")]
        public IActionResult GetById(Guid orgId)
        {
            var actionResult = this.organizationService.GetById(orgId);
            return Ok(actionResult);
        }

        /// <summary>
        /// 新增机构
        /// </summary>
        /// <param name="org">机构对象</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.AddEntity)]
        [Permission("org:add:entity")]
        public IActionResult Add([FromBody] SysOrganization org)
        {
            var actionResult = this.organizationService.Add(org);
            return Ok(actionResult);
        }

        /// <summary>
        /// 编辑机构
        /// </summary>
        /// <param name="org">机构对象</param>
        /// <returns>ActionResult</returns>
        [HttpPut]
        [Log(OperationType.EditEntity)]
        [Permission("org:edit:entity")]
        public IActionResult Put([FromBody] SysOrganization org)
        {
            var actionResult = this.organizationService.Update(org);
            return Ok(actionResult);
        }

        /// <summary>
        /// 删除机构
        /// </summary>
        /// <param name="orgId">对象编号</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{orgId}")]
        [Log(OperationType.DeleteEntity)]
        [Permission("org:del:entities")]
        public IActionResult Delete(Guid orgId)
        {
            if (this.Check(orgId))
            {
                return BadRequest("org contains users,can not be deleted.");
            }
            var actionResult = this.organizationService.Delete(orgId);
            return Ok(actionResult);
        }

        /// <summary>
        /// 批量删除机构
        /// </summary>
        /// <param name="ids">编号数组</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{ids}")]
        [Log(OperationType.DeleteEntity)]
        [Permission("org:del:entities")]
        public IActionResult DeleteRange(String ids)
        {
            var array = StringUtil.GetGuids(ids);
            foreach (var item in array)
            {
                if (this.Check(item))
                {
                    return BadRequest("org contains users,can not be deleted.");
                }
            }
            var actionResult = this.organizationService.DeleteRange(array);
            return Ok(actionResult);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns>QueryResult</returns>
        [HttpPost]
        [Log(OperationType.QueryList)]
        [Permission("role:query:list,user:query:list")]
        public IActionResult GetOrgUserTree()
        {
            var actionResult = this.organizationService.GetOrgUserTree();
            return Ok(actionResult);
        }

        /// <summary>
        /// 机构检测
        /// </summary>
        /// <param name="orgId">机构编号</param>
        /// <returns>真假值</returns>
        private bool Check(Guid orgId)
        {
            var queryCondition = new QueryCondition();
            queryCondition.QueryItems = new List<QueryItem>();
            queryCondition.QueryItems.Add(new QueryItem()
            {
                Field = "OrgId",
                DataType = DataType.Guid,
                QueryMethod = QueryMethod.Equal,
                Value = orgId
            });
            var data = this.organizationService.SqlQuery(queryCondition, "sqls:sql:query_sysuser");
            if (data.List.Count > 0)
            {
                return true;
            }
            return false;
        }
    }
}
